package com.hsogoo.avatar.registry.loadbanlance;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.google.common.collect.Lists;
import com.hsogoo.avatar.model.AvatarAddress;

/**
 * Created by za-huangsaigang on 2018/9/19.
 */
public class RoundRobinLoadBalancer implements LoadBalancer {

    private List<AvatarAddress> serverList = Lists.newArrayList();
    private final static AtomicInteger next = new AtomicInteger(0);

    public RoundRobinLoadBalancer(List<AvatarAddress> serverList){
        this.serverList = serverList;
    }

    @Override
    public AvatarAddress select() {
        int index = next.getAndIncrement() % serverList.size();
        return serverList.get(index);
    }

}
